 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testTennis
{
    public class Game
    {
        public enum Point
        { 
            love,
            fifteen,
            thirty,
            forty,
            gamepoint,
            deuce,
            advance
        }

        public Point scoreP1, scoreP2;
        public Player P1, P2;

       
        
        public Game()
        {
            P1 = new Player();
            P2 = new Player();
            scoreP1 = Point.love;
            scoreP2 = Point.love;
        }

        
      


        void updatePointGame(Player p)
        {
            if (p.Equals(P1))
            {
                switch (scoreP1.ToString())
                {
                    case "love": scoreP1 = Point.fifteen; break;
                    case "15": scoreP1 = Point.thirty; break;
                    case "30": scoreP1 = Point.forty; break;
                    case "40":
                        {
                            if (scoreP2.Equals(Point.love) || scoreP2.Equals(Point.fifteen) || scoreP2.Equals(Point.thirty))
                            {
                                scoreP1 = Point.gamepoint;
                                return;
                            }
                            else {
                                scoreP1 = Point.deuce;
                                scoreP2 = Point.deuce;
                            }
                        }
                        break;
                    case "deuce":
                        {
                            if (scoreP2.Equals(Point.advance))
                            {
                                scoreP1 = Point.deuce;
                                scoreP2 = Point.deuce;
                            }
                            else scoreP1 = Point.advance;
                        }
                        break;
                    case "Advance":
                        {
                            scoreP1 = Point.gamepoint;
                        }
                        break;
                }
            }
            else
                if (p.Equals(P2))
                {
                    switch (scoreP2.ToString())
                    {
                        case "0": scoreP2 = Point.fifteen; break;
                        case "15": scoreP2 = Point.thirty; break;
                        case "30": scoreP2 = Point.forty; break;
                        case "40":
                            {
                                if (scoreP1.Equals(Point.love) || scoreP1.Equals(Point.fifteen) || scoreP1.Equals(Point.thirty))
                                {
                                    scoreP2 = Point.gamepoint;
                                    return;
                                }
                                else
                                {
                                    scoreP2 = Point.deuce;
                                    scoreP1 = Point.deuce;
                                }
                            }
                            break;
                        case "deuce":
                            {
                                if (scoreP1.Equals(Point.advance))
                                {
                                    scoreP2 = Point.deuce;
                                    scoreP1 = Point.deuce;
                                }
                                else scoreP2 = Point.advance;
                            }
                            break;
                        case "Advance":
                            {
                                scoreP2 = Point.gamepoint;
                            }
                            break;
                    }
                }
        }

        void playgame()
        {
            while(scoreP1 != Point.gamepoint || scoreP2 != Point.gamepoint)
            {
                if (new Random().Next(0, 2) == 1)
                updatePointGame(P1);
                
            else
                updatePointGame(P2);
            }
        }

        public string winner()
        {
            //playgame();
            if (scoreP1 == Point.gamepoint)
                return "P1";
            return "P2";
        }
        }
}
